function insertPayload(data) {
  const props = Object.keys(data).join(",");
  const labels = new Array(Object.keys(data).length).fill("?").join(",");
  const values = Object.values(data);

  return {
    props,
    labels,
    values,
  };
}

function updatePayload(data) {
  const props = Object.keys(data)
    .map((item, index) => `${item} = ?`)
    .join(", ");
  const values = Object.values(data);
  return {
    props,
    values,
  };
}

function queryPayload(data, fuzzyQuery) {
  const fuzzyQueryIndex = [];

  const props = Object.keys(data)
    .map((item, index) => {
      if (fuzzyQuery.includes(item)) {
        fuzzyQueryIndex.push(index);
        return `${item} like ?`;
      } else {
        return `${item} = ?`;
      }
    })
    .join(" and ");

  const values = Object.values(data).map((item, index) => {
    if (fuzzyQueryIndex.includes(index)) {
      return `%${item}%`;
    } else {
      return item;
    }
  });

  return {
    props,
    values,
  };
}

module.exports = {
  insertPayload,
  queryPayload,
  updatePayload,
};
